package com.base.springboot.starter.rocketmq.service;

import cn.hutool.core.util.ObjectUtil;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * RockerMq 默认生产者IMPL
 *
 * @author YG
 * @date 2020/6/16 AM10:40
 */
public class RockerMqProducerService {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Value("${ecp.rocketMq.producer.topic.default}")
    private String defaultTopic;

    /**
     * rockerMqTemplate
     *
     * @return
     */
    public RocketMQTemplate getRocketMQTemplate() {
        return this.rocketMQTemplate;
    }

    /**
     * 默认主题，发送消息
     *
     * @param payload 消息体
     * @return
     */
    public SendResult syncSendDefault(Object payload) {
        if(ObjectUtil.isNull(payload)){
            return null;
        }
        return rocketMQTemplate.syncSend(defaultTopic + ":" + RocketMQHeaders.TAGS.toLowerCase(), payload);
    }

    /**
     * 指定主题，发送消息
     *
     * @param tag         标签
     * @param payload     消息体
     * @return
     */
    public SendResult syncSendDefault(String tag, Object payload) {
        if(ObjectUtil.isEmpty(tag) || ObjectUtil.isNull(payload)){
            return null;
        }
        return rocketMQTemplate.syncSend(defaultTopic + ":" + tag, payload);
    }

    /**
     * 指定主题及标签，发送消息
     *
     * @param destination 主题
     * @param tag         标签
     * @param payload     消息体
     * @return
     */
    public SendResult syncSend(String destination, String tag, Object payload) {
        if(ObjectUtil.isEmpty(destination) || ObjectUtil.isEmpty(tag) || ObjectUtil.isNull(payload)){
            return null;
        }
        return rocketMQTemplate.syncSend(destination + ":" + tag, payload);
    }
}